<!DOCTYPE html>
<html lang="en">

<head>
    <% include ../../commonHead.html %>
    <title>淘淘领</title>
    <link rel="stylesheet" type="text/css" href="/css/payback/detail.css?v20171101">
</head>

<body>
    <div class="banner">
        <div id="slider" class="swipe">
            <div class="swipe-wrap">
                <% for(var i in goods.picUrlArr) { %>
                    <div>
                        <img src="<%=goods.picUrlArr[i] %>" />
                    </div>
                <% } %>
            </div>
        </div>
        <nav>
            <ul id="position">
                <% for(var i in goods.picUrlArr) { %>
                    <% if(i == 0) { %>
                        <li class="on"></li>
                    <% } else { %>
                        <li></li>
                    <% } %>
                <% } %>
            </ul>
        </nav>
    </div>
    <div class="cnt-box">
        <% if(goods.leftSeconds > 0) { %>
        <div id="countdown" data-seconds="<%=goods.leftSeconds %>">
            开抢倒计时<em class="day">00</em><i>天</i><em class="hour">00</em><i>时</i><em class="minute">00</em><i>分</i><em class="second">00</em><i>秒</i>
        </div>
        <% } %>
        <div class="tit">

            <% if(goods.data_source == 'taobao' || goods.data_source == 'merchants' || goods.data_source == 'hsrj' ) { %>
              <% if(goods.taobao_link != ''){%>
                <em class="tag jsl">极速领</em>
              <% } else{%>
                <em class="tag tb">淘宝</em>
              <% } %>
            <% } else if(goods.data_source == 'meitianhui') { %>
            <em class="tag zy">官方</em>
            <% } %>

          <span><%=goods.title %></span>

        </div>
        <div class="price-box">
            <span class="final-price">到手价¥<em><%=goods.discount_price %></em></span>
            <!-- <span class="refund">预付¥<%=goods.market_price %>&nbsp;&nbsp;&nbsp;&nbsp;返还¥<%=(goods.market_price-goods.discount_price).toFixed(2) %></span> -->
            <span class="refund">预付¥<%=goods.market_price %>&nbsp;&nbsp;&nbsp;&nbsp;返还¥<%=(goods.market_price-goods.discount_price).toFixed(2) %></span>
        </div>
        <!-- 已售<em><%=goods.stock_qty-goods.sale_qty%></em>件 / -->
        <p class="sale-num"> 仅剩<em><%=goods.sale_qty %></em>件</p>
        <p class="tb_price">淘宝价 ￥ <%=goods.taobao_price %></p>
    </div>
    <div class="guide">
        <img src="/image/payback/guide.png?v2" />
    </div>
    <div class="desc"><%=goods.desc1 %></div>
    <% if(goods.picDetailArr && goods.picDetailArr.length > 0) { %>
    <div class="detail-wrap">
        <div>
            <img src="/image/payback/detail-bar.png" />
        </div>
        <% for(var i=0; i<goods.picDetailArr.length; i++) { %>
        <%if(goods.picDetailArr[i].title){%><p class="detail-desc"><%=goods.picDetailArr[i].title%></p><%}%>
        <div>
            <img src="<%=goods.picDetailArr[i].url %>" />
        </div>
        <% } %>
    </div>
    <% } %>
    <% if(!requestParams.s) { %>
    <div class="footer-bar">
        <a href="javascript: void(0);" class="chat" id="csBtn" onclick="getChatData();"></a>
        <%if(requestParams.mi){%><a href="javascript: collectGoods();" class="favor"></a><%}%>
        <% if(goods.sale_qty <= 0) { %>
            <a href="javascript: void(0);" class="btn disable">库存不足</a>
        <% } else if(goods.leftSeconds > 0) { %>
            <a href="javascript: void(0);" class="btn disable">敬请期待</a>
        <% } else if(alreadyGot == 'true') { %>
            <a href="javascript: void(0);" class="btn disable">限购一件</a>
        <% } else { %>
            <% if(goods.data_source=='taobao' || goods.data_source=='merchants' || goods.data_source=='hsrj') { %>
            <a href="javascript: void(0);" class="btn" id="goToSee">了解一下</a>
            <% } %>
            <a href="javascript: void(0);" class="btn able" id="getNow">立即领取</a>
        <% }%>
    </div>
    <% } else { %>
    <div style="width: 10rem; margin: 0 auto; margin-bottom: -2.666667rem; margin-top: 0.666667rem;">
        <img src="/image/fan/qr.gif?v2" style="width: 100%;" />
    </div>
    <img src="/image/common/qrcode_bdsh.png" style="width: 100%; height: 100%; position: fixed; top: 0; left: 0; opacity: 0;" />
    <% } %>
    <div class="upgrade-account">
        <h1 class="tit">礼券账户升级协议</h1>
        <h3 class="sub-tit">请先完成礼券账户升级<br>才能继续参与淘淘领活动</h3>
        <div class="asset">
            <p>礼券余额 <em id="voucher"></em></p>
            <p>兑换金币 <em id="gold"></em></p>
        </div>
        <p class="tip">
            提示：<br>
            金币是每天惠集团推出的体现会员权益和平台返利的虚拟货币，通用于各个业务体系，方便商家快速结算和整合资源提供优质服务。本通道为会员升级礼券账户，并完成礼券金币兑换(10:1)，操作完成后不可撤销，谢谢合作。
        </p>
        <div class="btn-wrap">
            <a href="javascript: void(0);" class="btn cancel">取消</a>
            <a href="javascript: void(0);" class="btn confirm">确认</a>
        </div>
    </div>
    <div class="bind-taobao-wrap">
        <div class="bind">
            <h1 class="tit">提示</h1>
            <h3 class="content">该账号尚未绑定淘宝会员名，请前去绑定后继续领取。</h3>
            <div class="btns">
                <a href="javascript: $('.bind-taobao-wrap').hide();" class="btn cancel">取消</a>
                <a href="#" class="btn go-bind">前去绑定</a>
            </div>
        </div>
    </div>
</body>
<script type="text/javascript">
var isCollecting = false;
var isUpgrading = false;
var isGetting = false;
var member_id = '<%=requestParams.mi%>';
var inBlacklist = '<%=inBlacklist%>';
var bind = '<%=requestParams.bind%>';
var goodsData = {
    goods_code : '<%=goods.goods_code %>',
    goods_url : '<%=goods.product_source %>',
    amount : '<%=goods.market_price %>',
    taobao_link: '<%-goods.taobao_link %>', //新增
    taobao_price:'<%=goods.taobao_price %>'//淘宝价
};
var chatData = {
    // 聊天id
    chat_id: 'llmcustomerserviceagent',
    // 聊天界面标题
    title: '官方客服',
    // 客服头像
    head_pic_path: 'https://oss-img.meitianhui.com/communityDocs/kefu.jpg',
    // 当前商品id
    goods_id: '<%=goods.goods_id %>',
    // 聊天来源
    from: 'llm'
};
var checkResult = {};
$(function() {
    M.initSwipe();

    // 开卖倒计时
    if($('#countdown').length > 0) {
        var difftime = parseInt($('#countdown').attr('data-seconds'));
        if(difftime > 0) {
            timer(difftime);
        }
    }

    var shareData = {
        title: '<%=goods.title %>',
        desc: '<%=goods.desc1 ? goods.desc1.replace(/\r\n/g," ").replace(/\n/g, " ") : "" %>',
        imgPath: '<%=goods.picUrlArr ? goods.picUrlArr[0] : "" %>',
        url: window.location.href + '&s=1'
    };

    if(window.jsObj) {  // Android
        // 传递分享数据
        if(window.jsObj.share) {
            window.jsObj.share(JSON.stringify(shareData));
        }

        // 前去看看
        $('#goToSee').click(function() {
            if(window.jsObj.openTaobao) {
                window.jsObj.openTaobao('<%-goods.product_source %>');
            }
            if(window.jsObj.openTaobaoLink) {
                // window.jsObj.openTaobaoLink('<%=goods.taobao_link %>','<%=goods.product_source %>');
                window.jsObj.openTaobaoLink( JSON.stringify({taobao_link:'<%-goods.taobao_link %>',product_source:'<%-goods.product_source %>'}));
            }
        });

        // 跳转至绑定淘宝账户
        $('.bind-taobao-wrap .go-bind').click(function() {
            if(window.jsObj.bindTaobaoMemberName) {
                $('.bind-taobao-wrap').hide();
                window.jsObj.bindTaobaoMemberName();
            }
        });
    } else {    // IOS
        setupWebViewJavascriptBridge(function(bridge) {
            // 传递分享数据
            bridge.callHandler('share', shareData);

            // 前去看看
            $('#goToSee').click(function() {
                bridge.callHandler('openTaobao', '<%-goods.product_source %>');
                // bridge.callHandler('openTaobaoLink', '<%=goods.taobao_link %>','<%=goods.product_source %>'); //新增
                bridge.callHandler('openTaobaoLink',  JSON.stringify({taobao_link:'<%-goods.taobao_link %>',product_source:'<%-goods.product_source %>'})); //新增
            });

            // 跳转至绑定淘宝账户
            $('.bind-taobao-wrap .go-bind').click(function() {
                $('.bind-taobao-wrap').hide();
                bridge.callHandler('bindTaobaoMemberName');
            });
        });
    }

    <% if(goods.sale_qty > 0) { %>
    $('.footer-bar').on('click', '#getNow', function() {
        if(inBlacklist == 'true') {
            M.Toast.warn('你的账号被加入黑名单，禁止参加此活动！<br/>请联系客服，谢谢。');
            return;
        }
        // 5秒内只能点击一次领取
        if(isGetting) return;
        isGetting = true;
        setTimeout(function() {
            isGetting = false;
        }, 5000);

        if(!member_id || (window.localStorage && window.localStorage['accountNotNeedUpgrade'+member_id])) {
            validTaobaoAccount();
        } else {
            // 判断礼券账户是否需要升级
            $.ajax({
                type: 'POST',
                url: '/openapi/h5/c/payback/checkVoucherAccount',
                data: {
                    member_id: member_id
                },
                success: function(checkData) {
                    var jsonData = typeof checkData == 'string' ? JSON.parse(checkData) : checkData;
                    if(jsonData.rsp_code == 'fail') {
                        M.Toast.warn('请检查当前网络是否可用');
                        return;
                    }
                    if(jsonData.data.is_exchange == 'N') {  // 不需要升级
                        if(window.localStorage) {
                            window.localStorage.setItem('accountNotNeedUpgrade'+member_id, true);
                        }
                        validTaobaoAccount();
                    } else {    // 需要升级
                        checkResult = jsonData;
                        $('.upgrade-account #voucher').text(checkData.data.voucher);
                        $('.upgrade-account #gold').text(checkData.data.gold);
                        $('.upgrade-account').show();
                        document.addEventListener('touchmove', preventDefault);
                    }
                },
                error: function() {
                    M.Toast.warn('请检查当前网络是否可用');
                }
            });
        }
    });
    <% } %>

    // 取消升级
    $('.upgrade-account .cancel').click(function() {
        $('.upgrade-account').hide();
        document.removeEventListener('touchmove', preventDefault);
    });

    // 确定升级
    $('.upgrade-account .confirm').click(function() {
        if(!isUpgrading) {
            $.ajax({
                type: 'POST',
                url: '/openapi/h5/c/payback/upgradeVoucherAccount',
                data: {
                    consumer_id: member_id,
                    mobile: checkResult.data.mobile,
                    voucher: checkResult.data.voucher,
                    gold: checkResult.data.gold
                },
                beforeSend: function() {
                    isUpgrading = true;
                    $('.upgrade-account .confirm').text('升级中');
                },
                success: function(checkData) {
                    var jsonData = typeof checkData == 'string' ? JSON.parse(checkData) : checkData;
                    if(jsonData.rsp_code == 'fail') {
                        M.Toast.warn(jsonData.error_msg);
                        return;
                    }
                    M.Toast.succ('礼券账户升级成功');
                    setTimeout(function() {
                        validTaobaoAccount();
                        $('.upgrade-account').hide();
                        document.removeEventListener('touchmove', preventDefault);
                    }, 2000);
                },
                complete: function() {
                    $('.upgrade-account .confirm').text('确定');
                    setTimeout(function() {
                        isUpgrading = false;
                    }, 3000);
                }
            });
        }
    });
});

// 获取聊天id
function getChatData() {
    if(!member_id) {
        callNativeToChat();
        return;
    }
    $.ajax({
        type: 'POST',
        url: '/openapi/h5/c/getChatIdOfDefaultStoreByConsumerId',
        data: {
            consumer_id: member_id,
            chat_id: 'llmcustomerserviceagent',
            title: '官方客服'
        },
        success: function(data) {
            data = typeof data == 'string' ? JSON.parse(data) : data;
            if(data.rsp_code == 'succ') {
                chatData.chat_id = data.data.chatId;
                chatData.title = data.data.title;
            }
        },
        complete: function() {
            callNativeToChat();
        }
    });
}


// 检查是否已绑定淘宝账户
function validTaobaoAccount() {
  // 修改 屏蔽
  callNativeToGet();
  return;
  //end 屏蔽
    if(!bind) { // 旧版本不做绑定逻辑，直接领取
        callNativeToGet();
        return;
    }
    if(window.jsObj) {
        window.jsObj.getTaobaoMemberName('getTaobaoAccountCb');
    } else {
        setupWebViewJavascriptBridge(function(bridge) {
            bridge.callHandler('getTaobaoMemberName', function(data) {
                getTaobaoAccountCb(data);
            });
        });
    }
}

// 检查淘宝账户绑定回调
function getTaobaoAccountCb(data) {
    data = typeof data == 'string' ? JSON.parse(data) : data;
    if(data.account_no) {
        callNativeToGet();
    } else {    // 提示绑定淘宝账户
        $('.bind-taobao-wrap').show();
    }
}

// 调Native方法聊天
function callNativeToChat() {
    if(window.jsObj) {
        window.jsObj.chatWithCustomerServiceAgent(JSON.stringify(chatData));
    } else {
        setupWebViewJavascriptBridge(function(bridge) {
            bridge.callHandler('chatWithCustomerServiceAgent', chatData);
        });
    }
}

// 调Native方法领取，5秒内只能调用一次
function callNativeToGet() {
  console.log('goodsData:',goodsData);
    if(window.jsObj) {
        if(window.jsObj.showSelectDialog) {
            window.jsObj.showSelectDialog(JSON.stringify(goodsData));
        }
    } else {
        setupWebViewJavascriptBridge(function(bridge) {
            bridge.callHandler('showSelectDialog', goodsData);
        });
    }
}

// 添加心愿单
function collectGoods() {
    if(!isCollecting) {
        $.ajax({
            type: 'POST',
            url: '/openapi/h5/c/collectGoods',
            data: {
                favorites_type: 'llm',
                member_id: '<%=requestParams.mi%>',
                goods_id: '<%=goods.goods_id%>',
            },
            beforeSend: function() {
                isCollecting = true;
            },
            success: function(data) {
                if(typeof data == 'string') data = JSON.parse(data);
                if(data.rsp_code == 'succ') {
                    M.Toast.succ('已添加至心愿单');
                }
            },
            complete: function() {
                setTimeout(function() {
                    isCollecting = false;
                }, 3000);
            }
        });
    }
}

// 倒计时
function timer(difftime){
    var id = setInterval(function(){
        if(difftime <= 0) {
            clearInterval(id);
            $('#countdown').remove();
            $('.disable').removeClass('disable').addClass('able').attr('id', 'getNow').text('立即领取');
        }
        var day=0, hour=0, minute=0, second=0;//时间默认值
        if(difftime > 0){
            day = Math.floor(difftime / (60 * 60 * 24));
            hour = Math.floor(difftime / (60 * 60)) - (day * 24);
            minute = Math.floor(difftime / 60) - (day * 24 * 60) - (hour * 60);
            second = Math.floor(difftime) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);
        }
        if (day <= 9) day = '0' + day;
        if (hour <= 9) hour = '0' + hour;
        if (minute <= 9) minute = '0' + minute;
        if (second <= 9) second = '0' + second;
        $('#countdown').find('.day').text(day);
        $('#countdown').find('.hour').text(hour);
        $('#countdown').find('.minute').text(minute);
        $('#countdown').find('.second').text(second);
        difftime--;
    }, 1000);
}

// 阻止默认事件
function preventDefault(e) {
    e.preventDefault();
    return;
}
</script>
</html>
